home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / LabelMenu / SetMenuTitle.c < prev   
Encoding:
C/C++ Source or Header  |  1995-06-16  |  2.7 KB  |  94 lines  |  [TEXT/MMCC]

  1. #include <menus.h>
  2. #include <types.h>
  3. #include <memory.h>
  4.  
  5.  
  6. void SetMenuTitle (MenuHandle theMenu, Str255 theNewTitle);
  7.  
  8.  
  9. void SetMenuTitle (MenuHandle theMenu, Str255 theNewTitle)
  10. {
  11.     Byte            oldTitleLength,        newTitleLength;
  12.     Ptr                oldItemLocation,    newItemLocation;
  13.     Size            oldHandleSize,        newHandleSize;
  14.     Ptr                theMenuAsAPtr;
  15.     Ptr                theDataAsAPtr;
  16.     Size            toMove;
  17.     short            difference;
  18.     char            theOldState;
  19.     
  20.     theOldState = HGetState((Handle)theMenu);    /* get the old menu handle state */
  21.     
  22.     HLock((Handle)theMenu);                        /* lock it */
  23.     theMenuAsAPtr = (Ptr)*theMenu;                /* offset to the menu data area is based on the */
  24.     theDataAsAPtr = (Ptr)&(**theMenu).menuData;    /* MenuInfo structure in Menus.h and Inside Mac */
  25.  
  26.     oldTitleLength = theDataAsAPtr[0];
  27.     newTitleLength = theNewTitle[0];
  28.     
  29.     difference = newTitleLength - oldTitleLength;
  30.     
  31.     oldHandleSize = GetHandleSize((Handle)theMenu);
  32.     newHandleSize = oldHandleSize + difference;
  33.  
  34.     /* If your titles are not all the same length, (i.e. difference != 0) */
  35.     /* then you are going to have to shift the items in the menu around, */
  36.     /* since the Menu Manager figures out where the items start based on string lengths */
  37.  
  38.         
  39.         if (difference > 0)
  40.         {
  41.             /* we have to grow the handle, so call SetHandleSize() then */
  42.             /* move the menu item date to make room for the longer title */
  43.  
  44.             HUnlock((Handle)theMenu);
  45.             SetHandleSize((Handle)theMenu, newHandleSize);
  46.  
  47.             /* check memerror here  */
  48.  
  49.             HLock((Handle)theMenu);
  50.             theMenuAsAPtr = (Ptr)*theMenu;
  51.             theDataAsAPtr = (Ptr)&(**theMenu).menuData;
  52.  
  53.             /* here, we get the location of the actual menu items */
  54.             /* by going past the header and the current title string */
  55.             
  56.             oldItemLocation = theDataAsAPtr + oldTitleLength + 1;
  57.             newItemLocation = theDataAsAPtr + newTitleLength + 1;
  58.             toMove = oldHandleSize - (oldItemLocation - theMenuAsAPtr);
  59.             BlockMove(oldItemLocation,newItemLocation,toMove);
  60.         }
  61.         
  62.         if (difference < 0)
  63.         {
  64.             /* we have to shrink the handle, so move the data first */
  65.             
  66.             oldItemLocation = theDataAsAPtr + oldTitleLength + 1;
  67.             newItemLocation = theDataAsAPtr + newTitleLength + 1;
  68.             toMove = oldHandleSize - (oldItemLocation - theMenuAsAPtr);
  69.             BlockMove(oldItemLocation,newItemLocation,toMove);
  70.             
  71.             /* now we can resize the menu handle */
  72.             
  73.             HUnlock((Handle)theMenu);
  74.             SetHandleSize((Handle)theMenu, newHandleSize);
  75.  
  76.             /* check memerror here  */
  77.  
  78.             HLock((Handle)theMenu);
  79.             theMenuAsAPtr = (Ptr)*theMenu;
  80.             theDataAsAPtr = (Ptr)&(**theMenu).menuData;;
  81.         }
  82.  
  83.     /* move the new title into place */
  84.     
  85.     BlockMove((Ptr)theNewTitle, theDataAsAPtr, newTitleLength+1);
  86.  
  87.     HSetState((Handle)theMenu,theOldState);        /* restore the original handle state */
  88.     
  89.     /* to insure redrawing, set and reset the menu bar  */
  90.  
  91.     SetMenuBar(GetMenuBar());
  92.     DrawMenuBar();
  93. }
  94.